package com.bawei.module_home.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class RedPacketProgressView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyle: Int = 0
) : View(context, attrs, defStyle) {
    private val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        color = 0xFFE0E0E0.toInt() // 浅灰色
        strokeWidth = 2f * resources.displayMetrics.density
    }
    private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        color = 0xFFFF3A3A.toInt() // 红色
        strokeWidth = 2f * resources.displayMetrics.density
        strokeCap = Paint.Cap.ROUND
    }
    private var progress = 0f // 0~1
    private val rectF = RectF()

    fun setProgress(p: Float) {
        progress = p.coerceIn(0f, 1f)
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val padding = progressPaint.strokeWidth / 2 + 2
        val size = Math.min(width, height).toFloat()
        rectF.set(padding, padding, size - padding, size - padding)
        // 背景圆环
        canvas.drawArc(rectF, 0f, 360f, false, bgPaint)
        // 进度圆环
        canvas.drawArc(rectF, -90f, 360f * progress, false, progressPaint)
    }
} 